from flask import Flask

import settings
from routes.chat_routes import chat
from routes.code_routes import code
from routes.group_routes import group
from routes.lesson_routes import lesson
from routes.resource_routes import resource
from routes.role_routes import role
from routes.scheduler_routes import scheduler
from routes.user_routes import user
from routes.worker_routes import worker
from util.ThreadPluginsInitUtil import cors, scheduler_obj
from util.ThreadPluginsInitUtil import db
from util.ThreadPluginsInitUtil import login_manager

queue_name = '/queue/ExamQueue'


class MyListener(object):
    """
    activeMQ消息监听嚣类
    """

    def on_message(self, headers, message):
        """
        接收消息成功执行的方法
        :param headers:     消息头
        :param message:     消息内容
        :return:
        """
        print('接收到headers: %s' % headers)
        print('接收到message: %s' % message)

    def on_error(self, headers, message):
        """
        接收消息失败执行的方法
        :param headers:     消息头
        :param message:     消息内容
        :return:
        """
        print('received an error headers: %s' % headers)
        print('received an error message: %s' % message)


def create_app(config_name):
    app = Flask(config_name)

    app.config['SQLALCHEMY_DATABASE_URI'] = settings.SQLALCHEMY_DATABASE_URI
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = settings.SQLALCHEMY_TRACK_MODIFICATIONS
    app.config['DEBUG'] = settings.DEBUG
    app.config['SQLALCHEMY_ECHO'] = settings.SQLALCHEMY_ECHO
    app.config['SECRET_KEY'] = settings.SECRET_KEY
    # app.config['SERVER_NAME'] = settings.SERVER_NAME

    app.logger.debug("数据库连接:{}".format(settings.SQLALCHEMY_DATABASE_URI))
    app.logger.debug("启动DEBUG:{}".format(settings.DEBUG))
    app.logger.debug("是否输入sql语句:{}".format(settings.SQLALCHEMY_ECHO))
    app.logger.debug("session密钥:{}".format(settings.SECRET_KEY))
    # app.logger.debug("访问地址:{}".format(settings.SERVER_NAME))

    login_manager.init_app(app)
    app.logger.debug("初使化登录插件login_manager......")
    db.init_app(app)
    app.logger.debug("初使化ORM插件login_manager......")
    cors.init_app(app, supports_credentials=True)
    app.logger.debug("初使化跨域访问插件login_manager......")

    scheduler_obj.start()

    # 注册user的蓝图
    app.register_blueprint(user)
    app.logger.debug("注册用户蓝图......")

    # 注册group的蓝图
    app.register_blueprint(group)
    app.logger.debug("注册机构蓝图......")

    # 注册resource的蓝图
    app.register_blueprint(resource)
    app.logger.debug("注册权限蓝图......")

    # 注册resource的蓝图
    app.register_blueprint(chat)
    app.logger.debug("注册微信蓝图......")

    # 注册role的蓝图
    app.register_blueprint(role)
    app.logger.debug("注册角色蓝图......")

    # 注册lesson的蓝图
    app.register_blueprint(lesson)
    app.logger.debug("注册课程蓝图......")

    # 注册自动生成代码的蓝图
    app.register_blueprint(code)
    app.logger.debug("注册自动生成代码蓝图......")

    # 注册自动生成代码的蓝图
    app.register_blueprint(scheduler)
    app.logger.debug("注册定时任务蓝图......")

    app.register_blueprint(worker)
    app.logger.debug("注册工作量管理蓝图......")

    app.logger.debug("创建flask app 成功.....................")
    return app
